package com.tanker.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @comments
 * @author  zg
 * @version 创建时间：2014年5月23日
 */
public class TimeSeparater {
	/** 一秒钟毫秒数  */
	private static final long  MILLISECONDS_PER_SECONDS = 1000;
	/** 一分钟毫秒数  */
	private static final long  MILLISECONDS_PER_MINUTE = 1000 * 60;
	/** 一小时毫秒数  */
	private static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60;
	/** 一天毫秒数  */
	private static final long MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * 24;
	/** 一周毫秒数  */
	private static final long MILLISECONDS_PER_WEEK = MILLISECONDS_PER_DAY * 7;

	private int weeks;//周数
	private int days;//天数
	private int hours;//小时数
	private int minutes;//分钟数

	public static final String ENV = "";

	/*public TimeUnitSeparater(long startTime, long endTime) throws ParseException{
		long startMilliSeconds = CommonConstants.DATETIME_SEC_STR.parse(String.valueOf(startTime)).getTime();
		long endMilliSeconds = CommonConstants.DATETIME_SEC_STR.parse(String.valueOf(endTime)).getTime();
		long timeRange = endMilliSeconds - startMilliSeconds;

		this.weeks = (int) (timeRange/WEEK_MILLISECONDS);//得到周数
		this.days = (int) ((timeRange - WEEK_MILLISECONDS * weeks)/DAY_MILLISECONDS);//得到天数
		this.hours = (int) ((timeRange - WEEK_MILLISECONDS * weeks - DAY_MILLISECONDS * days)/HOUR_MILLISECONDS);//得到小时数
		this.minutes = (int) ((timeRange - WEEK_MILLISECONDS * weeks - DAY_MILLISECONDS * days - HOUR_MILLISECONDS * hours)/MINUTE_MILLISECONDS);//得到分钟数
	}*/

	/**
	 * 按自然日正常进位（24小时为一天，7天为一周）,计算时间段有多少周零几天零几小时零几分钟
	 * @param startTime yyyyMMddHHmmss
	 * @param endTime yyyyMMddHHmmss
	 * @throws ParseException
	 */
	public TimeSeparater natural(long startTime, long endTime) throws ParseException{
		long timeRange = getTimeRange(startTime, endTime);

		this.weeks = (int) (timeRange / MILLISECONDS_PER_WEEK);//得到周数
		long allWeeksMss = MILLISECONDS_PER_WEEK * weeks;

		this.days = (int) ((timeRange - allWeeksMss) / MILLISECONDS_PER_DAY);//得到天数
		long allDaysMss = MILLISECONDS_PER_DAY * days;

		this.hours = (int) ((timeRange - allWeeksMss - allDaysMss) / MILLISECONDS_PER_HOUR);//得到小时数
		long allHoursMss = MILLISECONDS_PER_HOUR * hours;

		this.minutes = (int) ((timeRange - allWeeksMss - allDaysMss - allHoursMss) / MILLISECONDS_PER_MINUTE);//得到分钟数

		return this;
	}

	/**
	 * 以“天”为单位，得到“几天几小时几分钟”
	 * @param startTime
	 * @param endTime
	 * @throws ParseException
	 */
	public TimeSeparater days(long startTime, long endTime) throws ParseException{
		long timeRange = getTimeRange(startTime, endTime);

		this.days = (int) (timeRange / MILLISECONDS_PER_DAY);//得到天数
		long allDaysMss = MILLISECONDS_PER_DAY * days;

		this.hours = (int) ((timeRange - allDaysMss) / MILLISECONDS_PER_HOUR);//得到小时数
		long allHoursMss = MILLISECONDS_PER_HOUR * hours;

		this.minutes = (int) ((timeRange - allDaysMss -allHoursMss) / MILLISECONDS_PER_MINUTE);//得到分钟数

		return this;
	}

	public static String formatDayTimeRange(long startTime, long endTime) throws ParseException{
		StringBuilder timeFormat = new StringBuilder("");
		long timeRange = getTimeRange(startTime, endTime);

		int days = (int) (timeRange / MILLISECONDS_PER_DAY);//得到天数
		long allDaysMss = MILLISECONDS_PER_DAY * days;

		int hours = (int) ((timeRange - allDaysMss) / MILLISECONDS_PER_HOUR);//得到小时数
		long allHoursMss = MILLISECONDS_PER_HOUR * hours;

		int minutes = (int) ((timeRange - allDaysMss -allHoursMss) / MILLISECONDS_PER_MINUTE);//得到分钟数

		if(days != 0){
			timeFormat.append(days);
			timeFormat.append("天");
		}

		if(hours != 0){
			timeFormat.append(hours);
			timeFormat.append("小时");
		}

		if(minutes != 0){
			timeFormat.append(minutes);
			timeFormat.append("分");
		}
		return timeFormat.toString();
	}
	/**
	 * 获取HH:mm:ss
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	public static Long formatHoureTimeRange(long startTime, long endTime) throws ParseException{
		long timeRange = getTimeRange(startTime, endTime);
		long seconds = (int) (timeRange/MILLISECONDS_PER_SECONDS);//得到秒数
		return seconds;
	}
	/**
	 * 以“小时”为单位，得到“几小时几分钟”
	 * @param startTime
	 * @param endTime
	 * @throws ParseException
	 */
	public TimeSeparater hours(long startTime, long endTime) throws ParseException{
		long timeRange = getTimeRange(startTime, endTime);

		this.hours = (int) (timeRange / MILLISECONDS_PER_HOUR);//得到小时数
		long allHoursMss = MILLISECONDS_PER_HOUR * hours;

		this.minutes = (int) ((timeRange - allHoursMss) / MILLISECONDS_PER_MINUTE);//得到分钟数
		return this;
	}

	public double getDelayRevertHours(long startTime, long endTime) throws ParseException{
		TimeSeparater ts = hours(startTime,endTime);
		double hour = ts.getHours();
		int minute = ts.getMinutes();
		if(minute <= 30){
			hour = hour + 0.5;  //加半小时
		}else{
			hour = hour + 1;  //超过30分钟，算1个小时。
		}
		return hour;
	}

	/**
	 * 计算v41延迟派单的时间
	 * @param currentTime  当前时间，时间一直在缩短，靠近起租时间。
	 * @param rentTime  起租时间
	 * @return
	 * @throws ParseException
	 */
	public int getDelayDistributeHours(long currentTime, long rentTime,float distributeDeadlineTime) throws ParseException{
		if(currentTime >= rentTime){ //到了起租时间，不进入调度，直接设置为平台取消。
			return -2;
		}
		TimeSeparater ts = hours(currentTime,rentTime);  //时间差
		double hour = ts.getHours();  //小时数
		int minute = ts.getMinutes();  //需要带上分钟。
		if(minute >= 30){
			hour = hour + 0.5;  //加半小时
		}
		//可用时间
		double useHour = hour - distributeDeadlineTime; //总小时数减去派单预留时间。
		if(useHour > 24){
			if("1".equals(ENV)){
				return 24*60*60;  //24小时
			}else{
				//仅仅测试
				return 300; //5分钟   5*60
			}
		}else if(useHour >= 1){
			if("1".equals(ENV)){
				return 1*60*60;   //1小时
			}else{
				//仅仅测试
				return 180; //3分钟  3*60
			}
//		}else if(useHour >= 0){   //去掉
//			return 0; //立即执行
		}else if(useHour < 1 && useHour > 0){   //去掉，不能出现空档。
//			return -3; //取消执行  //30分钟执行
			if("1".equals(ENV)){
				return 30*60;	//30分钟
			}else{
				//仅仅测试
				return 120; //2分钟  2*60
			}
		}else if(useHour <= 0 && useHour >= -0.5){   //-0.5 <= useHour 效率优先最后一次执行。  LAST执行一次       // 4~4.5小时进入最后一次执行。
			if("1".equals(ENV)){
				return 0;  //立即执行
			}else{
				return 60; //1分钟
			}
		}else if(useHour < -0.5 && useHour >= -4.5){	//4小时进入人工调度。
			return -1; //超时不派单，需要进入调度处理了。
		}else{
			return -2; //到了起租时间了。
		}
//		if(minute <= 30){
//			hour = hour + 0.5;  //加半小时
//		}else{
//			hour = hour + 1;  //超过30分钟，算1个小时。
//		}
//		return hour;
	}


	public static double getMonthSpace(Date beginDate,Date endDate){
		Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(beginDate);
        c2.setTime(endDate);
        int month = (c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR)) * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);


        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        if(day1 > day2){
        	month = month - 1;
        }
        double dm = 0;
        if((day1 - day2) != 0){
        	dm = new BigDecimal(30-Math.abs(day1-day2)).divide(new BigDecimal(30),2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }

		return month+dm;
	}


	public static void main(String[] args) throws ParseException{
		//TimeSeparater ts = new TimeSeparater().days(20140624100000l, 20140625100000l);
//		System.out.println(formatDayTimeRange(20140624100000l, 20140625100000l));
//		System.out.println("\ndone!");

//		long i = getTimeRange(20141016190000l,20141016200000l);
//		System.out.println("i:" + i);

		TimeSeparater ts = new TimeSeparater();
		/*ts.hours(20150511000000l,20150522220000l);
		System.out.println("h:" + ts.getHours());
		System.out.println("m:" + ts.getMinutes());
		System.out.println(formatHoureTimeRange(20150511000000l,20150522220000l));

		System.out.println("秒1="+getTimeRange(20170413150000l, 20170413151500l));
		System.out.println("秒2="+getTimeRange(20170413150000l, 20170413151500l) / 1000);*/


		int delay = ts.getDelayDistributeHours(20170421143100l, 20170421143000l, 4.5f);
		System.out.println("delay="+delay);
//
//		double d = ts.getDelayRevertHours(20141016181500l, 20141016202529l);
//		System.out.println("d:" + d);

		/*TimeSeparater ts2 = new TimeSeparater();
		ts2.days(20140613221500l, 20140615221500l);
		System.out.println("d:" + ts2.getDays());
		System.out.println("h:" + ts2.getHours());
		System.out.println("m:" + ts2.getMinutes());*/
	}

	//========================== private methods ============================
	public static long getTimeRange(long startTime, long endTime) throws ParseException{
		long startMilliSeconds = new SimpleDateFormat("yyyyMMddHHmmss").parse(String.valueOf(startTime)).getTime();
		long endMilliSeconds = new SimpleDateFormat("yyyyMMddHHmmss").parse(String.valueOf(endTime)).getTime();
		return endMilliSeconds - startMilliSeconds;
	}


	public int getWeeks() {
		return weeks;
	}
	public void setWeeks(int weeks) {
		this.weeks = weeks;
	}
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	public int getMinutes() {
		return minutes;
	}
	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}



}

